package cn.tedu.pet.passport.security;

import cn.tedu.pet.passport.mapper.AdminMapper;
import cn.tedu.pet.passport.pojo.vo.AdminLoginInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
       log.debug("Spring Security框架自动调用了UserDetailsServiceImpl.loadUserByUsername()方法 ,用户名：{}", s);
        AdminLoginInfoVO loginInfo =adminMapper.getLoginInfoByUsername(s);
        if (loginInfo == null) {
            log.debug("此用户名没有匹配的用户数据，将返回null");
            return null;
        }
        log.debug("用户名匹配成功！准备返回此用户名匹配的UserDetails类型的对象");
        List<String> permissions = loginInfo.getPermissions();
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        for (String permission : permissions) {
            GrantedAuthority authority = new SimpleGrantedAuthority(permission);
            authorities.add(authority);
        }


        AdminDetails userDetails = new AdminDetails(
                loginInfo.getId(), loginInfo.getUsername(), loginInfo.getPassword(),
          loginInfo.getAvatar(),  loginInfo.getEnable() == 1, authorities);
        log.debug("userDetails {}",  userDetails);
//       UserDetails userDetails = User.builder()
//               .username(s)
//               .password(loginInfo.getPassword())
//               .disabled(loginInfo.getEnable()==0)
//               .accountLocked(false)
//               .accountExpired(false)
//               .credentialsExpired(false)
//               .authorities("这是一个临时使用的山寨的权限！！！")
//               .build();
       log.debug("即将向Spring Security返回UserDetails类型的对象：{}", userDetails);
       return userDetails;
    }
}
